const app = new Vue({
    el: '#manBox',
    data: function () {
        return {
            isShow: 1,
            form1: {
                name: '',
                batchNum: '',
                approvalNum: '',
                traceableCode: '',
                mfd: '', //生产日期
                exp: '', //有效期至
                std: '', //入库时间
                other: ''
            },
            form2: {
                name: [],
                manName: '',
                batchNum: [],
                traceableCode: '',
                ostd: '',
                buyer: [],
                other: ''
            },
            selectForm: {
                drugName: '',
                manName: '',
                batchNum: '',
                traceableCode: '',
                ostd: '',
                buyer: '',
                other: ''
            },
            form3: {
                traceableCode: '',
                name: '',
                batchNum: '',
                beginDate: '',
                endDate: '',
                manName: '',
            },
            manTable: [],
            traTable: [],
            form4: {
                userId: '',
                username: '',
                phoneNumber: '',
                manName: '',
                address: '',
                legal: '',
                contact: ''
            },
            pwdForm: {
                oldPwd: '',
                newPwd: ''
            },
            phoForm: {
                param: ''
            },
            legForm: {
                param: ''
            },
            conForm: {
                param: ''
            },
            pwdOpen: false,
            phoOpen: false,
            legOpen: false,
            conOpen: false,
            formLabelWidth: '120px'
        }
    },
    methods: {
        change1: function () {
            this.isShow = 1;
        },
        change2: function () {
            this.isShow = 2;
            // 获取药品生产商名称和该药品生产商生产的所有药品的名称
            // 获取所有销售商名称
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.get('/info/drugName', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.form2.name = res.data.data.drugNames;
                        that.form2.manName = res.data.data.manName;
                        that.form2.buyer = res.data.data.buyers;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        change3: function () {
            this.isShow = 3;
            //获取企业名称
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.get('/user/currentUser', {headers: {'Authorization': token}}).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.form3.manName = res.data.data.bindName;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        change4: function () {
            let that = this;
            that.isShow = 4;
            //获取手机号码、企业名称、注册地址、法定代表人、联系方式
            let token = window.localStorage.getItem('userToken');
            axios.get('/info/getMan', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.form4.username = res.data.data.username;
                        that.form4.phoneNumber = res.data.data.phoneNumber;
                        that.form4.manName = res.data.data.manName;
                        that.form4.address = res.data.data.address;
                        that.form4.legal = res.data.data.legal;
                        that.form4.contact = res.data.data.contact;
                    } else if (code === 1004) {
                        console.log(token);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        logout: function () {
            let token = window.localStorage.getItem('userToken');
            axios.get('/logout', {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        location.href = '../../index.html';
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        cleanForm: function () {
            if (this.isShow === 1) {
                this.form1 = {
                    name: '',
                    batchNum: '',
                    approvalNum: '',
                    mfd: '',
                    exp: '',
                    std: '',
                    other: ''
                }
            }
            if (this.isShow === 3) {
                this.form3 = {
                    traceableCode: '',
                    name: '',
                    batchNum: '',
                    approvalNum: '',
                    beginDate: '',
                    endDate: ''
                }
            }
        },
        onSubmit1: function () {
            //提交药品生产数据
            if (this.form1.name === ''
                || this.form1.batchNum === ''
                || this.form1.approvalNum === ''
                || this.form1.traceableCode === ''
                || this.form1.mfd === ''
                || this.form1.exp === ''
                || this.form1.std === '') {
                this.$message.warning('请填写完整数据！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/submit/mand', that.form1,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.$message.success('保存成功！');
                        that.cleanForm();
                    } else if (code === 1001) {
                        that.$message.warning('数据出错，未保存成功！');
                    } else if (code === 1008) {
                        that.$message.warning('药品溯源编码已存在，未保存成功！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        selectDrug: function () {
            //根据选择的药品名称，获取该药品的所有药品批号
            let that = this;
            axios.post('/info/batch', {param: that.selectForm.drugName}).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.form2.batchNum = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectForm.drugName);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        getCode: function () {
            //根据药品名称和药品批号获取药品溯源编码
            let that = this;
            axios.post('/info/code', {
                first: that.selectForm.drugName,
                second: that.selectForm.batchNum
            }).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.form2.traceableCode = res.data.data;
                    } else if (code === 1001) {
                        console.log(that.selectForm.drugName);
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        onSubmit2: function () {
            //提交药品运输数据
            this.selectForm.manName = this.form2.manName;
            this.selectForm.traceableCode = this.form2.traceableCode;
            this.selectForm.ostd = this.form2.ostd;
            this.selectForm.other = this.form2.other;
            if (this.selectForm.drugName === ''
                || this.selectForm.manName === ''
                || this.selectForm.batchNum === ''
                || this.selectForm.traceableCode === ''
                || this.selectForm.ostd === ''
                || this.selectForm.buyer === '') {
                this.$message.warning('请填写完整数据！');
                return;
            }

            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/submit/trad', that.selectForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        console.log(res);
                        that.$message.success('保存成功！');

                    } else if (code === 1001) {
                        that.$message.warning('数据出错，未保存成功！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        },
        search: function () {
            //溯源生产数据
            let that = this;
            axios.post('/search/mand', that.form3).then(
                function (res) {
                    console.log(res);
                    let code = res.data.code;
                    if (code === 200) {
                        that.manTable = [];
                        that.manTable = res.data.data;
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )

            //溯源运输数据
            axios.post('/search/trad', that.form3).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.traTable = [];
                        that.traTable = res.data.data;
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )


        },
        cancelChangePwd: function () {
            this.pwdForm = {
                oldPwd: '',
                newPwd: ''
            }
            this.pwdOpen = false;
        },
        changePwd: function () {
            //修改密码
            if (this.pwdForm.oldPwd === '' || this.pwdForm.newPwd === '') {
                this.$message.warning('密码不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changePwd', that.pwdForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功，请重新登录！');
                        location.href = '../../index.html';
                    } else if (code === 1007) {
                        that.$message.error('原始密码输入错误，请重试！');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.pwdOpen = false;
        },
        cancelChangePho: function () {
            this.phoForm.newPhone = '';
            this.phoOpen = false;
        },
        changePho: function () {
            //修改手机号码
            if (this.phoForm.param === '') {
                this.$message.warning('内容不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changePho', that.phoForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功！');
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.phoOpen = false;
        },
        cancelChangeLegal: function () {
            this.legForm.newLegal = '';
            this.legOpen = false;
        },
        changeLegal: function () {
            //修改法定代表人
            if (this.legForm.param === '') {
                this.$message.warning('内容不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changeLegal', that.legForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功！');
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.legOpen = false;
        },
        cancelChangeContact: function () {
            this.conForm.newContract = '';
            this.conOpen = false;
        },
        changeContact: function () {
            //修改联系方式
            if (this.conForm.param === '') {
                this.$message.warning('内容不能为空！');
                return;
            }
            let that = this;
            let token = window.localStorage.getItem('userToken');
            axios.post('/info/changeContact', that.conForm,
                {headers: {'Authorization': token}}).then(
                function (res) {
                    let code = res.data.code;
                    if (code === 200) {
                        that.$message.success('修改成功！');
                    } else if (code === 1001) {
                        console.log('参数错误');
                    }
                }, function (err) {
                    console.log(err);
                }
            )
            this.conOpen = false;
        }
    },
})